﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UCNKompetenceBors.Database;
using UCNKompetenceBors.Model;

namespace UCNKompetenceBors.User
{
    public partial class profileDetailsSale : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindSubMenu();
                BindGridView();

                #region Globalization
                string culture = String.Empty;

                if (HttpContext.Current.Session["MyCulture"] != null)
                    culture = HttpContext.Current.Session["MyCulture"].ToString();

                if (!String.IsNullOrEmpty(culture))
                    GeneralTools.SetCulture(culture);

                LocalizeControls();
                #endregion
            }
        }

        /// <summary>
        /// Binds the sub menu links.
        /// </summary>
        private void BindSubMenu()
        {
            hyLinkProfile.NavigateUrl = "~/User/profileDetails.aspx?uid=" + Request.QueryString["uid"];
            hyLinkSale.NavigateUrl = "~/User/profileDetailsSale.aspx?uid=" + Request.QueryString["uid"];
            hyLinkPurchases.NavigateUrl = "~/User/profileDetailsPurchase.aspx?uid=" + Request.QueryString["uid"];
        }

        /// <summary>
        /// Binds the gvprofileDetailsPurchase GridView.
        /// </summary>
        private void BindGridView()
        {
            gvProfileDetailsSale.DataSource = dbProfile.GetUserSales(Guid.Parse(Request.QueryString["uid"]));
            gvProfileDetailsSale.DataBind();
        }

        private void LocalizeControls()
        {
            hyLinkProfile.Text = GetLocalResourceObject("hyLinkProfileText").ToString();
            hyLinkPurchases.Text = GetLocalResourceObject("hyLinkPurchasesText").ToString();
            hyLinkSale.Text = GetLocalResourceObject("hyLinkSaleText").ToString();

            LoginUser user = dbLogin.GetUserById(Guid.Parse(Request.QueryString["uid"]));

            lblHeader.Text = user.UserName + "\'s " + GetLocalResourceObject("lblHeaderText").ToString();

            gvProfileDetailsSale.Columns[0].HeaderText = GetLocalResourceObject("gvProfileDetailsSaleTitleText").ToString();
            gvProfileDetailsSale.Columns[1].HeaderText = GetLocalResourceObject("gvProfileDetailsSaleCategoryText").ToString();
            gvProfileDetailsSale.Columns[2].HeaderText = GetLocalResourceObject("gvProfileDetailsSaleDateText").ToString();
            gvProfileDetailsSale.Columns[3].HeaderText = GetLocalResourceObject("gvProfileDetailsSaleStatusText").ToString();

            gvProfileDetailsSale.DataBind();
        }

        protected void gvProfileDetailsSale_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                foreach (Label lbl in e.Row.Cells[3].Controls.OfType<Label>())
                {
                    lbl.Text = (Boolean.Parse(lbl.Text)) ? GetLocalResourceObject("StatusActive").ToString() : GetLocalResourceObject("StatusInActive").ToString();
                }

                foreach (Label lbl in e.Row.Cells[1].Controls.OfType<Label>())
                {
                    string culture = String.Empty;

                    if (HttpContext.Current.Session["MyCulture"] != null)
                        culture = HttpContext.Current.Session["MyCulture"].ToString();

                    Sale sale = (Sale)e.Row.DataItem;

                    if (culture.Equals("en-GB"))
                        lbl.Text = sale.Category.LocalizedCategoryName;
                    else
                        lbl.Text = sale.Category.CategoryName;
                }
            }
        }
    }
}